<?php

namespace App\Store\Controllers\Sysset;

use App\Common\Services\Store\Common as StoreCommon;
use App\Store\Models\Sysset\Config;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Store\Validate\Sysset\Config as ValidateConfig;
use Illuminate\Support\Facades\Redis;

/**
 * 配置
 */
class ConfigController extends Controller
{
    /**
     * 新增
     * sign_in 签到
     *
     */
    public function dopostset(Request $request)
    {
        $req_data = $request->only(['config_key', 'config_val']);
        $Validate = new ValidateConfig('set');
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }

        // 组装数据
        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $cp_id = $accountinfo['cp_id'];

        $config_data = [
            'cp_id'      => $cp_id,
            'config_key' => $req_data['config_key'],
            'config_val' => $req_data['config_val'],
        ];
        if($req_data['config_key']=='sign_in'){
            Redis::set('pointssetting_'.$cp_id,$req_data['config_val']);
        }


        Config::updateOrInsert(['cp_id' => $cp_id, 'config_key' => $req_data['config_key']], $config_data);
        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }

    /**
     * 详情
     */
    public function dopostget(Request $request)
    {
        $req_data = $request->only(['config_key']);
        $Validate = new ValidateConfig('get');
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }

        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $cp_id = $accountinfo['cp_id'];

        $one = Config::where(['cp_id' => $cp_id, 'config_key' => $req_data['config_key']])->first();
        if ($one) {
            $one = $one->toArray();
            return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $one]);
        }
        return response()->json(['code' => '000', 'msg' => '获取失败']);
    }

    /**
     * 通过积分获得兑换的钱
     */
    public function getmoney(Request $request)
    {
        $req_data = $request->only(['score']);
        $Validate = new ValidateConfig('getmoney');
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }

        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $cp_id = $accountinfo['cp_id'];

        $one = Config::where(['cp_id' => $cp_id, 'config_key' => 'sign_in'])->first();
        if ($one) {
            $one = $one->toArray();
            $config_val = json_decode($one['config_val'], true);
            if (isset($config_val['rate'])) {
                $money = $req_data['score'] / $config_val['rate'];
                return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => ['money' => $money]]);
            } else {
                return response()->json(['code' => '000', 'msg' => '没有设置兑换比例']);
            }
        }
        return response()->json(['code' => '000', 'msg' => '获取失败']);
    }
}
